package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.util.Scanner;

/**
 * 工作模式:一个队列,一个生产者,多个消费者,轮询
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();//通信通道
        //定义队列
        c.queueDeclare("task_queue",
                true,//队列设置成持久队列->服务器上已经存在的队列属性不可变
                false, false, null);
        //发送消息
        while (true){
            System.out.println("请输入消息:");
            String msg = new Scanner(System.in).nextLine();
            c.basicPublish("", "task_queue",
                    MessageProperties.PERSISTENT_TEXT_PLAIN,//设置消息是持久消息
                    msg.getBytes());
        }
    }
}
